package cn.tomoki.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.tomoki.beans.ItemBeanAppoint;
import cn.tomoki.myapplication.R;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class AppointViewAdapter extends RecyclerView.Adapter<AppointViewAdapter.InnerHolker> {

    private final List<ItemBeanAppoint> mDate;
    private  OnItemClickListener onItemClickListener;

    public AppointViewAdapter(List<ItemBeanAppoint> data) {
        this.mDate = data;
    }


    @NonNull
    @NotNull
    @Override
    public InnerHolker onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_appoint_history, null);
        return new InnerHolker(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull InnerHolker holder, int position) {
        holder.setData(mDate.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (mDate != null){
            return  mDate.size();
        }
        return 0;
    }

    //提供设置接口的方法
    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }
    //创建一个接口，定义内部的方法
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    public class InnerHolker extends RecyclerView.ViewHolder{
        private ImageView outNext;
        private TextView appointTimeText;
        private TextView appointNameText;
        private TextView appointNumberText;
        private int mPosition;

        public InnerHolker(View itemView) {
            super(itemView);
            outNext = itemView.findViewById(R.id.list_next);
            appointTimeText = itemView.findViewById(R.id.list_appointTime);
            appointNameText = itemView.findViewById(R.id.list_appointName);
            appointNumberText =  itemView.findViewById(R.id.list_appointNumber);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null){
                        onItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }
        public void setData(ItemBeanAppoint itemBeanAppoint, int position) {
            this.mPosition = position;
            outNext.setImageResource(itemBeanAppoint.icon);
            appointTimeText.setText((CharSequence) itemBeanAppoint.appointTime);
            appointNameText.setText(itemBeanAppoint.appointName);
            appointNumberText.setText(itemBeanAppoint.appointNumber);
        }
    }
}
